package com.learn.springbootcache.controller;

import com.learn.springbootcache.bean.Employee;
import com.learn.springbootcache.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Jmy
 */
@RestController
public class EmployeeController {

    private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);

    @Autowired
    EmployeeService employeeService;

    @GetMapping("/emp/{id}")
    public Employee getEmpById(@PathVariable("id") Integer id){
        Employee employee = employeeService.getEmpById(id);
        if (employee != null) {
            logger.info("请求用户信息数据，根据用户ID:{}", employee.toString());
            return employee;
        }
        logger.info("用户信息不存在，用户ID：{}", id);
        return null;
    }

    @GetMapping("/updateEmp")
    public Employee updateEmployee(Employee employee){
        Employee emp = employeeService.updateEmp(employee);
        return emp;
    }

    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id") Integer id){
        employeeService.delEmployee(id);
        return "success";
    }

    @GetMapping("/emp/name/{name}")
    public List<Employee> getEmpByLastName(@PathVariable("name") String name){
        return employeeService.getEmpByName(name);
    }
}
